#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
"""
四、购物车
功能要求：
要求用户输入总资产，例如：2000
显示商品列表，让用户根据序号选择商品，加入购物车
购买，如果商品总额大于总资产，提示账户余额不足，否则，购买成功。
附加：可充值、某商品移除购物车
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
"""

goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]

buy = []
# 帐户金额，购物车金额
count_asset = 0
while True:
    print("1、购买商品\n2、结账\n3、充值\n4、移除购物车\n")
    chiose = input("请输入选项: ")

    if chiose in ["1", "2", "3", "4"]:
        if chiose == "1":
            while True:
                print("商品列表--------> ")
                for kindex, key in enumerate(goods): print(kindex, key["name"])
                try:
                    buy_chiose = int(input("请输入要购买的物品序号 (任意键退出购买): "))
                    if buy_chiose >= len(goods):
                        print("没有这个商品，退出购买")
                        break
                except:
                    print("退出购买")
                    break
                buy.append(goods[buy_chiose])
                print("当前购买的商品有 {}".format([buys["name"] for buys in buy]))

        elif chiose == "2":
            buy_asset = 0
            if buy:
                price = [buys["price"] for buys in buy]
                for i in price: buy_asset += i
                if buy_asset <= count_asset:
                    print("购买成功, 商品列表: {}, 剩余金额: {}".format([buys["name"] for buys in buy], count_asset - buy_asset))
                    buy.clear()
                else:
                    print("金额不足以付款, 请充值或删除商品，当前商品金额: {}".format(buy_asset))
            else:
                print("没商品，请先去购买")

        elif chiose == "3":
            asset = int(input("充值的金额: ").strip())
            count_asset += asset
            print("当前金额： {}".format(count_asset))
        elif chiose == "4":
            print("移除商品")
    else:
        print("输入错误")
        continue
